<template>
    <el-card shadow="never" class="card-border-none">
        <list-header :permissions="{
            create:'createNotice,POST',
            refresh:'getNoticeList,GET'
            }"
            @create="handleCreate"
            @refresh="getData"
        ></list-header>

        <el-tree id="list-table" :draggable="false"
        :data="tableData" :props="{ children: 'child', label: 'name' }" @node-click="()=>{}" default-expand-all :expand-on-click-node="false">
            <template #default="{ node, data }">
                <div class="custom-tree-node">
                    <span class="custom-tree-node-label">{{ node.label }}</span>
                    <span class="custom-tree-node-span">

                        <el-button v-permission="['getCategoryGoods,GET']"
                        v-if="data.category_id == 0" link type="primary" size="small" class="span-pubilc" @click="openGoodsDrawer(data.id)">
                            推荐产品
                        </el-button>

                        <el-switch v-permission="['updateCategoryStatus,POST']"
                        @change="handleStatusChange($event,data)" :active-value="1" :inactive-value="0" :model-value="data.status" :loading="data.statusLoading" class="span-pubilc"/>

                        <el-button v-permission="['updateCategory,POST']"
                        class="span-pubilc" link type="primary" size="small" @click="handleEdit(data)">修改</el-button>

                        <!-- <el-button v-permission="['createCategory,POST']"
                        class="span-pubilc" link type="primary" size="small" @click="addChild(data.id)">增加</el-button> -->

                        <el-popconfirm title="是否要删除该记录？" @confirm="handleDelete(data.id)" confirm-button-text="是" cancel-button-text="否">
                            <template #reference>
                                <el-button v-permission="['getCategoryGoods,GET']"
                                link type="primary" size="small">删除</el-button>
                            </template>
                        </el-popconfirm>

                    </span>
                </div>
            </template>
		</el-tree>

        <form-drawer
        ref="formDrawerRef"
        :title="drawerTitle"
        :showActions="true"
        :destroyOnClose="true"
        @submit="handleSubmit">
            <el-form :model="form" ref="formRef" :rules="rules" label-width="80px" :inline="false" size="default">
                <el-form-item label="分类名称" prop="name">
                    <el-input v-model="form.name" placeholder="分类名称"></el-input>
                </el-form-item>
            </el-form>
        </form-drawer>

        <goods-drawer ref="GoodsDrawerRef"></goods-drawer>
    </el-card>
</template>
<script setup>
import { ref } from 'vue'
import ListHeader from '~/components/ListHeader.vue'
import FormDrawer from '~/components/FormDrawer.vue'
import GoodsDrawer from './components/GoodsDrawer.vue'
import { 
    createCategory,
    updateCategory,
    getCategoryList,
    deleteCategory,
    updateCategoryStatus
} from '~/api/category'
import { useInitTable,useInitForm } from '~/composables/useCommon'

// 初始化表格数据
const {
    // 表格数据
    tableData,
    // 获取数据方法
    getData,
    handleDelete,
    handleStatusChange
} = useInitTable({
    // 获取数据的方法
    getList:getCategoryList,
    success:(res)=>{
        tableData.value = res.map((o) => {
            o.statusLoading = false;
            return o
        })
    },
    // 删除/批量删除api
    deleteData:deleteCategory,
    // 修改状态api
    updateStatus:updateCategoryStatus,
})

// 初始化表格
const {
    drawerTitle,
    form,
    formRef,
    rules,
    formDrawerRef,
    handleSubmit,
    handleCreate,
    handleEdit
} = useInitForm({
    // 表单初始数据
    form:{
        id: null,
        name:"",
        category_id:0,
        status:1
    },
    // 表单验证规则
    rules:{
        name: [{
            required: true,
            message: "分类名称不能为空",
            trigger: 'blur'
        }]
    },
    // 获取数据方法
    getData,
    // 新增数据api
    createData:createCategory,
    // 修改数据api
    updateData:updateCategory
})

// 查看推荐产品
const GoodsDrawerRef = ref(null)
const openGoodsDrawer = id => GoodsDrawerRef.value.open(id)
</script>
<style lang="scss" scoped>
.card-border-none {
    border: none;
    .custom-tree-node {
    flex: 1;
    display: flex;
    align-items: center;
    .custom-tree-node-label {
      margin-left: 8px;
    }
    .custom-tree-node-span {
      margin-left: auto;
      .span-pubilc {
        margin-right: 18px;
      }
    }
  }
}
:deep(.el-card__body) {
    background: #fff;
    border-radius: 4px;
}
:deep(.el-tree-node__content) {
  padding: 8px 0;
}
</style>